<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Models\Shop\Product,App\Models\Shop\ProductGallery,App\Models\Shop\Brand;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Shop\ProductCategory as Category;


class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    private function attributes()
    {
        view()->share([
            'categories' => Category::get_categories(),
            'brands' => Brand::orderBy('sort_order')->get(),
            'filter_categories' => Category::filter_categories()
        ]);
    }
    
    

   public function index()
    {

        $products = Product::with('category')
            ->with('brand')
            ->get();
        //$products->attributes();
        //return $products;
        return view('admin.shop.products.index ')->with('products', $products);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::where('parent_id',0)->get();
        $brands = Brand::all();
        //return $categories;
        return view('admin.shop.products.create')->with('brands',$brands)->with('categories',$categories);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //return $request->imgs();
        $data = $request->except(['stock', 'brand_id', 'file', 'imgs']);
        $data['stock'] = $request->stock == '无限' ? '-1' : $request->stock;
        $data['brand_id'] = $request->brand_id == '-1' ? '' : $request->brand_id;

       $product = Product::create($data);

        //相册
        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $product-> product_galleries()->create(['img' => $img]);
            }
        }
        return redirect(route('admin.product.index'))->with('success', '新增成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product = Product::find($id);
        //return $product;
        $categories = Category::all();
        //$brands = Brand::where(id=)
        return view('admin.shop.products.edit')->with('product',$product)->with('categories',$categories);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //return $request->all();exit;
        $product = Product::find($id);
        $product->update($request->all());
        return redirect(route('admin.product.index'))->with('success', '编辑品牌成功~');
    }

    /**
     * 删除所选
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    function destroy($id)
    {
        if (!Product::check_orders($id)) {
            return back()->with('error', '当前商品拥有对应的订单，无法删除~');
        }
        Product::destroy($id);
        return back()->with('success', '被删商品已进入回收站~');
    }

    public function force_destroy($id)
    {
        Product::withTrashed()->where('id', $id)->forceDelete();
        ProductGallery::where('product_id', $id)->delete();
        return back()->with('info', '删除成功');
    }

    /**
     * 多选删除
     * @param Request $request
     */
    function destroy_checked(Request $request)
    {
        $checked_id = $request->input("checked_id");
        $delete_id = [];

        //检测商品是否能删除
        foreach ($checked_id as $id) {
            if (Product::check_orders($id)) {
                $delete_id[] = $id;
            }
        }

        Product::destroy($delete_id);
    }


    function force_destroy_checked(Request $request)
    {
        $checked_id = $request->input("checked_id");
        ProductGallery::whereIn('product_id', $checked_id)->delete();
        Product::withTrashed()->whereIn('id', $checked_id)->forceDelete();
    }

    /*
     * 还原商品
     */
    public function restore($id)
    {
        Product::withTrashed()->where('id', $id)->restore();
        return back()->with('info', '还原成功');

    }


    public function restore_checked(Request $request)
    {
        $checked_id = $request->input("checked_id");
        Product::withTrashed()->whereIn('id', $checked_id)->restore();
        return back()->with('info', '还原成功');

    }

    /**
     * 商品回收站
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function trash()
    {
        $products = Product::with('category')->with('brand')->onlyTrashed()->paginate(config('xSystem.page_size'));
        return view('admin.xShop.products.trash', ['products' => $products, '_product_trash' => 'am-active', '_product' => '']);
    }

    //ajax删除相册图片
    function destroy_gallery(Request $request)
    {
        ProductGallery::destroy($request->gallery_id);
    }

    //修改库存
    function change_stock(Request $request)
    {
        $product = Product::find($request->id);
        $product->stock = $request->stock;
        $product->save();
    }

    /**
     * ajax修改is...属性
     * @param Request $request
     */
    function is_something(Request $request)
    {
        $attr = $request->attr;
        $product = Product::find($request->id);
        $value = $product->$attr ? false : true;
        $product->$attr = $value;
        $product->save();
    }
}
